﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using StockMonitor.Configuration;
using System.Windows.Forms;
using System.IO;
using Tigera.Framework.Utility;
using System.ComponentModel;
using Tigera.T_Definition;

namespace StockMonitor
{
    [Serializable]
    public class EntrustRecord
    {
        /// <summary>
        /// 委托时间
        /// </summary>
        [DisplayNameAttribute("委托时间")]
        public int ReportTime { get; set; }
        /// <summary>
        /// 证券代码
        /// </summary>
        [DisplayNameAttribute("证券代码")]
        public string StockCode { get; set; }
        /// <summary>
        /// 证券名称
        /// </summary>
        [DisplayNameAttribute("证券名称")]
        public string StockName { get; set; }
        /// <summary>
        /// 买卖方向
        /// </summary>
        [DisplayNameAttribute("买卖方向")]
        public EntrustBs Entrustbs { get; set; }
        /// <summary>
        /// 委托编号
        /// </summary>
        [DisplayNameAttribute("委托编号")]
        public int EntrustNo { get; set; }
        /// <summary>
        /// 委托状态
        /// </summary>
        [DisplayNameAttribute("委托状态")]
        public Enums.EntrustStatus EntrustStatus { get; set; }
        /// <summary>
        /// 委托价格
        /// </summary>
        [DisplayNameAttribute("委托价格")]
        public double EntrustPrice { get; set; }
        /// <summary>
        /// 委托数量
        /// </summary>
        [DisplayNameAttribute("委托数量")]
        public double EntrustAmount { get; set; }
        /// <summary>
        /// 成交数量
        /// </summary>
        [DisplayNameAttribute("成交数量")]
        public double BusinessAmount { get; set; }
        /// <summary>
        /// 委托方式 0 自助委托；1 电话委托；2 驻留委托；3 远程委托；4 柜台委托；5 漫游委托； 6 分支委托； 7 Internet委托
        /// </summary>
        [DisplayNameAttribute("委托方式")]
        public Enums.OpEntrustWay OpEntrustWay { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        [DisplayNameAttribute("时间戳")]
        public DateTime TimeStamp { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:13 PM</datetime>
        /// </remarks>
        public override string ToString()
        {
            return string.Format("以￥{5} 委托{1} {2}({3}) {4}股，委托单号：{0}", EntrustNo, Entrustbs, StockCode, StockName, EntrustAmount, EntrustPrice.ToString("F2"));
        }

        /// <summary>
        /// 显示委托处理结果，是否成功
        /// </summary>
        /// <returns>字符串</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:15 PM</datetime>
        /// </remarks>
        public string ShowResult()
        {
            if (this.EntrustStatus == Enums.EntrustStatus.已成 || this.EntrustStatus == Enums.EntrustStatus.部成)
            {
                return string.Format("委托：{0}{1}，委托{2}{3}({4}){5}股，成交{6}股", EntrustNo, EntrustStatus, Entrustbs, StockCode, StockName, EntrustAmount, BusinessAmount);
            }
            return string.Format("委托：{0},目前状态为{1}", EntrustNo, EntrustStatus);
        }
    }

    /// <summary>
    /// 委托列表；同时负责将记录保存到文件
    /// </summary>
    public class EntrustList : List<EntrustRecord>
    {
        static string Path = Application.StartupPath + AppMgr.GetValue<string>("EntrustSavePath");

        EntrustList(List<EntrustRecord> lst)
        {
            this.AddRange(lst);
        }

        /// <summary>
        /// 保存列表中的委托
        /// </summary>
        public void Save()
        {
            Save(DateTime.Today);
        }

        /// <summary>
        /// 保存列表中某天的委托到文件
        /// </summary>
        /// <param name="dt"></param>
        public void Save(DateTime dt)
        {
            try
            {
                string txt = Tigera.Framework.SerializerUtility.XmlSerialize(this).Trim();
                File.WriteAllText(Path + dt.ToString("yyyyMMdd"), txt);
            }
            catch (Exception e)
            {
                //Log.Exception("保存委托列表失败", e);
            }
        }

        /// <summary>
        /// 加载当日委托
        /// </summary>
        /// <returns></returns>
        public static EntrustList Load()
        {
            return Load(DateTime.Today);
        }

        /// <summary>
        /// 加载某天的委托
        /// </summary>
        /// <param name="dt">具体日期</param>
        /// <returns>委托列表</returns>
        public static EntrustList Load(DateTime dt)
        {
            List<EntrustRecord> list = new List<EntrustRecord>();
            //if (File.Exists(Path + dt.ToString("yyyyMMdd")))
            //{
            //    string txt = File.ReadAllText(Path + dt.ToString("yyyyMMdd")).Trim();
            //    if (txt == string.Empty)
            //        list = new List<EntrustRecord>();
            //    else
            //        list = CLFramework.SerializerUtility.XmlDeserialize<List<EntrustRecord>>(txt);
            //}
            return new EntrustList(list);
        }

        /// <summary>
        /// 查询一个时间段内的委托
        /// </summary>
        /// <param name="dtBegin">开始日期</param>
        /// <param name="dtEnd">结束日期</param>
        /// <returns>委托列表</returns>
        public static List<EntrustRecord> Query(DateTime dtBegin, DateTime dtEnd)
        {
            List<EntrustRecord> list = new List<EntrustRecord>();

            for (DateTime i = dtBegin; i <= dtEnd; i = i.AddDays(1))
            {
                list.AddRange(Load(i));
            }

            return list;
        }

        /// <summary>
        /// 添加委托记录，并保存到文件
        /// </summary>
        /// <param name="item">待添加的委托实体</param>
        public new void Add(EntrustRecord item)
        {
            base.Add(item);
            this.Save();
        }
        /*
        /// <summary>
        /// 调用接口更新最新状态
        /// </summary>
        public void UpdateStatus()
        {
            TradeContext.Instance.UpdateTodayEntrusts();
            this.Save();
        }
         */ 
    }
}
